package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TOrg;
import com.wit.witmg.entity.TSite;
import com.wit.witmg.mapper.TSiteMapper;
import com.wit.witmg.service.ITSiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2024-12-03
 */
@Service
public class TSiteServiceImpl extends ServiceImpl<TSiteMapper, TSite> implements ITSiteService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        TSite t= JSON.parseObject(json,TSite.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            t.setDate(new Date());
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, String bnp, String bn) {
        QueryWrapper<TSite> qw=new QueryWrapper<>();
        if(bnp!=null){
            qw.eq("bnp", bnp);
            List<TSite> list= list(qw);
            if(list==null) {
                list= new ArrayList<>();
            }
            return JsonResult.success(list);
        }else if(bn!=null){
            qw.like("bn", bn);
            qw.last("limit 1");
            return JsonResult.success(getOne(qw));
        }else{
            List<TSite> allNodes = list();
            List<TSite> rootNodes = new ArrayList<>();
            for (TSite node : allNodes) {
                if (node.getPid() == null || node.getPid() == 0) {
                    rootNodes.add(node);
                }
            }
            for (TSite rootNode : rootNodes) {
                buildTree(rootNode, allNodes);
            }
            return JsonResult.success(rootNodes);
        }

    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            QueryWrapper<TSite> qw=new QueryWrapper<>();
            qw.eq("pid",id);
            remove(qw);
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }

    private void buildTree(TSite parentNode, List<TSite> allNodes) {
        List<TSite> children = new ArrayList<>();
        for (TSite node : allNodes) {
            if (node.getPid() != null && node.getPid().equals(parentNode.getId())) {
                children.add(node);
            }
        }
        if (!children.isEmpty()) {
            parentNode.setChildren(children);
            for (TSite child : children) {
                buildTree(child, allNodes);
            }
        }
    }
}
